<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <!-- 数据列表 -->
    <el-card class="mt10" :body-style="{ padding: '10px' }">
      <div slot="header" class="mb9 f16">
        <span style="font-weight: 600">裁断完工扫描记录</span>
      </div>
      <Table stripe :tableHeader="tableHeader" :tableData="tableData" :summaryData="summaryData" summary :isLoading="isLoading" />
      <!-- 分页器 -->
      <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, watch } from 'vue'
import { reactive, ref } from 'vue'
import 'dayjs/locale/zh-cn'
import { TableList, selectList } from '@/plugins/mixin'
import type { TableListType } from '@/plugins/mixin'
import type { formListType } from '@/components/Find/index.vue'
import fileRequest from '@/utils/fileRequest'
import { debounce } from 'lodash'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'CuttingMass'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Api from '@/api/GeneralManager/inquire/CuttingMass'
const isLoading = ref(false)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 7,
    num: 0
  },
  {
    index: 10,
    num: 0
  }
])
const getTableList = debounce(async () => {
  isLoading.value = true
  let { Data, dataList, srcList } = (await TableList(formList, Api.getTableList, currentPage.value, pageSize.value)) as any
  tableData.value = dataList
  total.value = Data.total
  imgList.value = srcList
  summaryData.value[0].num = Data.orderTotal
  summaryData.value[1].num = Data.proTotal
  isLoading.value = false
}, 500)
const getSelectList = async () => {
  await selectList(formList, Api.getSelectList)
}

const imgList = ref<string[]>([])

const formList = ref<any>([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入货号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '采集类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '客户交期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '完成日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '生产员工',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])

const tableData = ref([])

// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取下拉菜单数据

onMounted(async () => {
  getTableList()
  getSelectList()
  console.log(await Api.getSelectList())
})
// 表格数据
const tableHeader = ref([
  {
    label: '采集类型',
    prop: 'prodType'
  },
  {
    width: '140px',
    label: '指令号',
    prop: 'lotNo'
  },
  {
    label: '品牌',
    prop: 'brand'
  },
  {
    width: '120px',
    label: '工厂货号',
    prop: 'customStyle'
  },
  {
    label: '颜色',
    prop: 'styleColor'
  },
  {
    label: '号码',
    prop: 'sizeNo'
  },
  {
    label: '订单数量',
    prop: 'orderQty'
  },
  {
    label: '客户交期',
    prop: 'deliveryDate'
  },
  {
    label: '生产员工',
    prop: 'workerName'
  },
  {
    label: '生产数量',
    prop: 'prodQty'
  },
  {
    label: '采集员',
    prop: 'prodUserName'
  },
  {
    width: '120px',
    label: '厂商&责任单位',
    prop: 'dutyDept'
  },
  {
    width: '120px',
    label: '备注&补单原因',
    prop: 'dutyReason'
  },
  {
    label: '扫描时间',
    prop: 'prodTime'
  },
  {
    type: 'img',
    label: '图片',
    fixed: true,
    prop: 'photoPath',
    srcList: imgList
  }
])

watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
      item.checked = true
    })
    getTableList()
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/yield/downloadCutYield?choose=${1}`, {
      brand: formList.value.find((item: any) => item.selectLabel && Jzhcn.Find[item.selectLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
        ?.selectValue,
      completeEndTime: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '完成日期'
      )?.dateValue?.[1],
      completeStartTime: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '完成日期'
      )?.dateValue?.[0],
      customStyle: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入货号'
      )?.inputValue,
      deliveryEndTime: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
      )?.dateValue?.[1],
      deliveryStartTime: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
      )?.dateValue?.[0],
      dutyDept: formList.value.find(
        (item: any) => item.selectLabel && Jzhcn.Find[item.selectLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商'
      )?.selectValue,
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
      )?.inputValue,
      type: formList.value.find(
        (item: any) => item.selectLabel && Jzhcn.Find[item.selectLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '采集类型'
      )?.selectValue,
      workName: formList.value.find(
        (item: any) => item.selectLabel && Jzhcn.Find[item.selectLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '生产员工'
      )?.selectValue
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    link.download = `裁断产量查询导出${Date.now()}.xls` //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
</script>

<style lang="less" scoped></style>
